//
//  PopoverAnimDelegate.m
//  TEST
//
//  Created by beisheng03 on 2017/9/25.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "PopoverAnimDelegate.h"
#import "PopPresentController.h"



@implementation PopoverAnimDelegate


#pragma mark- 转场代理
- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    
    return [[PopPresentController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
    
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    
    return  self;
    
}

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    
    return 0.25;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    UIView *presentView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    if (presentView) {
        
        [[transitionContext containerView] addSubview:presentView];
        
        presentView.transform = CGAffineTransformMakeTranslation(0, KScreenH * 0.6);
        [transitionContext containerView].backgroundColor = [UIColor whiteColor];
        
        //执行动画
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            presentView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
            
        }];
        
        
        
        
    } else {
        //取出消失的View
        UIView *disView =  [transitionContext viewForKey:UITransitionContextFromViewKey];
        
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            
            disView.transform = CGAffineTransformMakeTranslation(0, KScreenH * 0.6);

        }completion:^(BOOL finished) {
            
            [transitionContext completeTransition:YES];
            
        }];
        
        
        
        
    }
    
}


@end
